#include "tester.h"
#include "ui_tester.h"

#include <math.h>
#include <QtAlgorithms>
#include "config.h"

Tester::Tester(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Tester)
{
    ui->setupUi(this);

    m_msg = "hello";
    m_currentResTimeList.clear();
    m_autoConnectTimer = new QTimer(this);
    m_autoSendMsgTimer = new QTimer(this);
    m_autoDisconnectTimer = new QTimer(this);
    m_updateDisplayTimer = new QTimer(this);
    connect(m_autoConnectTimer, SIGNAL(timeout()),
            this, SLOT(slotAutoConnectTimer()));
    connect(m_autoSendMsgTimer, SIGNAL(timeout()),
            this, SLOT(slotAutoSendMsgTimer()));
    connect(m_autoDisconnectTimer, SIGNAL(timeout()),
            this, SLOT(slotAutoDisconnectTimer()));
    connect(m_updateDisplayTimer, SIGNAL(timeout()),
            this, SLOT(slotUpdateDisplayTimer()));
}

Tester::~Tester()
{
//    foreach(QString str, m_uIdList)
//    {
//        if(m_socketMap.contains(str.toInt()))
//        {
//            delete m_socketMap[str];
//        }
//    }
    delete ui;
}

void Tester::slotAutoConnectTimer(void)
{
    MsgSocket *socket = new MsgSocket(ui->le_ipAddr->text(), ui->le_port->text().toInt());
    if(socket != nullptr)
    {
        m_socketMap.insert(Config::m_userId, socket);
        m_uIdList.append(QString::number(Config::m_userId));
        ui->cb_choose->addItem(QString::number(Config::m_userId++));
        ui->lcd_connect->display(++Config::m_socketConnectNumber);
        Config::m_socketOnlineNumber = Config::m_socketConnectNumber - Config::m_socketDisconnectNumber;
        ui->lcd_online->display(Config::m_socketOnlineNumber);
    }
}

void Tester::slotAutoDisconnectTimer(void)
{
    Config::m_socketOnlineNumber = Config::m_socketConnectNumber - Config::m_socketDisconnectNumber;
    ui->lcd_online->display(Config::m_socketOnlineNumber);
    if(Config::m_socketOnlineNumber > 0)
    {
        int index = qrand()%Config::m_socketOnlineNumber;
        int uid = m_uIdList.at(index).toInt();
        if(m_socketMap.contains(uid))
        {
            delete m_socketMap[uid];
            m_socketMap.remove(uid);
            ui->cb_choose->removeItem(uid);
            ui->lcd_disconnect->display(++Config::m_socketDisconnectNumber);
        }
    }
}

void Tester::slotAutoSendMsgTimer(void)
{
    if(Config::m_socketOnlineNumber > 0)
    {
        int index = qrand()%Config::m_socketOnlineNumber;
        int uid = m_uIdList.at(index).toInt();
        if(m_socketMap.contains(uid))
        {
            m_socketMap[uid]->slotSendMsg(Config::m_msgID++, m_msg);
        }
    }
}

void Tester::slotUpdateDisplayTimer(void)
{
    if(Config::m_packetSendNumber > 10000)
    {
        ui->lcd_sendPack->display(Config::m_packetSendNumber/10000);
        ui->lb_sendN->setText(tr("/万"));
    }else
    {
        ui->lcd_sendPack->display(Config::m_packetSendNumber);
    }

    if(Config::m_packetRecvNumber > 10000)
    {
        ui->lcd_recvPack->display(Config::m_packetRecvNumber/10000);
        ui->lb_recvN->setText(tr("/万"));
    }else
    {
        ui->lcd_recvPack->display(Config::m_packetRecvNumber);
    }

    ui->lcd_lostPack->display(Config::m_packetSendNumber - Config::m_packetRecvNumber);

    ui->lcd_sendRate->display(Config::m_rateSendTPS);
    ui->lcd_recvRate->display(Config::m_rateRecvTPS);

    m_currentResTimeList.swap(Config::m_resTimeList);
    Config::m_resTimeList.clear();
    double resTime = 0;
    foreach(int t, m_currentResTimeList)
    {
        resTime += t;
    }

    ui->lcd_respTime->display(resTime/m_currentResTimeList.length());
    m_currentResTimeList.clear();
    Config::m_rateSendTPS = 0;
    Config::m_rateRecvTPS = 0;
}

void Tester::on_pb_connect_clicked()
{
    MsgSocket *socket = new MsgSocket(ui->le_ipAddr->text(), ui->le_port->text().toInt());
    if(socket != nullptr)
    {
        m_socketMap.insert(Config::m_userId, socket);
        m_uIdList.append(QString::number(Config::m_userId));
        ui->cb_choose->addItem(QString::number(Config::m_userId++));
        ui->lcd_connect->display(++Config::m_socketConnectNumber);
        Config::m_socketOnlineNumber = Config::m_socketConnectNumber - Config::m_socketDisconnectNumber;
        ui->lcd_online->display(Config::m_socketOnlineNumber);
    }
}

void Tester::on_pb_disconnect_clicked()
{
    Config::m_socketOnlineNumber = Config::m_socketConnectNumber - Config::m_socketDisconnectNumber;
    ui->lcd_online->display(Config::m_socketOnlineNumber);
    if(Config::m_socketOnlineNumber > 0)
    {
        int index = qrand()%Config::m_socketOnlineNumber;
        int uid = m_uIdList.at(index).toInt();
        if(m_socketMap.contains(uid))
        {
            delete m_socketMap[uid];
            m_socketMap.remove(uid);
            ui->cb_choose->removeItem(uid);
            ui->lcd_disconnect->display(++Config::m_socketDisconnectNumber);
        }
    }
}

void Tester::on_pb_sendMsg_clicked()
{
    if(Config::m_socketOnlineNumber > 0)
    {
        int index = qrand()%Config::m_socketOnlineNumber;
        int uid = m_uIdList.at(index).toInt();
        if(m_socketMap.contains(uid))
        {
            m_socketMap[uid]->slotSendMsg(Config::m_msgID++, ui->le_msg->text());
        }
    }
}

void Tester::on_pb_autoTest_clicked()
{
    if(ui->cbx_autoConnect->isChecked())
    {
        m_autoConnectTimer->start(100);
    }else
    {
        m_autoConnectTimer->stop();
    }

    if(ui->cbx_autoDisconnect->isChecked())
    {
        m_autoDisconnectTimer->start(200);
    }else
    {
        m_autoDisconnectTimer->stop();
    }

    if(ui->cbx_autoSendMsg->isChecked())
    {
        m_autoSendMsgTimer->start(20);
    }else
    {
        m_autoSendMsgTimer->stop();
    }

    m_updateDisplayTimer->start(1000);
}
